﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ProcessoSeletivo.DAO
{
    public class Sala : IDisposable
    {
        #region " Listar "

        public DataView ListarSala()
        {
            DataSet lRetorno = new DataSet();
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ListarSala", sql);
                sqlc.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter sqa = new SqlDataAdapter(sqlc);
                sqa.Fill(lRetorno, "tb_pse_sala");
                sql.Close();
            }
            return lRetorno.Tables[0].DefaultView;
        }

        #endregion

        #region " Consultar "

        public DataView ConsultarSala(int cod_sala_sal)
        {
            DataSet lRetorno = new DataSet();
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ConsultarSala", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_sala_sal", cod_sala_sal);

                SqlDataAdapter sqa = new SqlDataAdapter(sqlc);
                sqa.Fill(lRetorno, "tb_pse_sala");
                sql.Close();
            }
            return lRetorno.Tables[0].DefaultView;
        }

        #endregion

        #region " Incluir "

        public int IncluirSala(DataSet dsRegistro)
        {
            int PrimaryKey = 0;

            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("IncluirSala", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@des_sala_sal", dsRegistro.Tables[0].Rows[0]["des_sala_sal"]);
                sqlc.Parameters.AddWithValue("@num_sala_sal", dsRegistro.Tables[0].Rows[0]["num_sala_sal"]);
                sqlc.Parameters.AddWithValue("@flg_pne_sal", dsRegistro.Tables[0].Rows[0]["flg_pne_sal"]);
                sqlc.Parameters.AddWithValue("@flg_leitor_sal", dsRegistro.Tables[0].Rows[0]["flg_leitor_sal"]);
                sqlc.Parameters.AddWithValue("@qtd_capacidade_sal", dsRegistro.Tables[0].Rows[0]["qtd_capacidade_sal"]);

                PrimaryKey = Convert.ToInt32(sqlc.ExecuteScalar().ToString());
                sql.Close();
            }

            return PrimaryKey;
        }

        #endregion

        #region " Alterar "

        public void AlterarSala(DataSet dsRegistro)
        {
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("AlterarSala", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@des_sala_sal", dsRegistro.Tables[0].Rows[0]["des_sala_sal"]);
                sqlc.Parameters.AddWithValue("@num_sala_sal", dsRegistro.Tables[0].Rows[0]["num_sala_sal"]);
                sqlc.Parameters.AddWithValue("@flg_pne_sal", dsRegistro.Tables[0].Rows[0]["flg_pne_sal"]);
                sqlc.Parameters.AddWithValue("@flg_leitor_sal", dsRegistro.Tables[0].Rows[0]["flg_leitor_sal"]);
                sqlc.Parameters.AddWithValue("@qtd_capacidade_sal", dsRegistro.Tables[0].Rows[0]["qtd_capacidade_sal"]);
                sqlc.Parameters.AddWithValue("@cod_sala_sal", dsRegistro.Tables[0].Rows[0]["cod_sala_sal"]);   
                sqlc.ExecuteNonQuery();

                sql.Close();
            }
        }

        #endregion

        #region " Excluir "

        public void ExcluirSala(int cod_sala_sal)
        {
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ExcluirSala", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_sala_sal", cod_sala_sal);
                sqlc.ExecuteNonQuery();

                sql.Close();
            }
        }

        #endregion

        #region " Dipose "

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
